using System.Collections.Generic;
using DataModel;
using RitaReports.Travels;

namespace RitaReports.Expenses
{
    public class ExpensesDataSource : TravelsDataSource
    {
        private readonly List<Organization> _organizations = new List<Organization>();
       
        readonly Dictionary<int, ExpensesItem> _costsByOrgId = new Dictionary<int, ExpensesItem>();
        readonly List<ExpensesItem> _orgCosts = new List<ExpensesItem>();

        readonly Dictionary<string, ExpensesItem> _costsBySeniority = new Dictionary<string, ExpensesItem>();
        readonly List<ExpensesItem> _seniorityCosts = new List<ExpensesItem>();


        readonly Dictionary<string, ExpensesItem> _costsByDestination = new Dictionary<string, ExpensesItem>();
        readonly List<ExpensesItem> _destinationCosts = new List<ExpensesItem>();

        public List<ExpensesItem> Organizations
        {
            get { return _orgCosts; }
        }

        public List<ExpensesItem> Seniorities
        {
            get { return _seniorityCosts; }
        }

        public List<ExpensesItem> Destinations
        {
            get { return _destinationCosts; }
        }

        public ExpensesDataSource(IEnumerable<Organization> organizations, IEnumerable<ReportHistoryDescr> list)
            : base(list)
        {
            _organizations.AddRange(organizations);

            foreach (ReportHistoryDescr descr in list)
            {
                if (_costsByOrgId.ContainsKey(descr.SbmtOrgId) == false)
                {
                    Organization org = GetOrg(descr.SbmtOrgId);
                    _costsByOrgId.Add(descr.SbmtOrgId, new ExpensesItem());
                    _costsByOrgId[descr.SbmtOrgId].Name = (org == null) ? "?" : org.Name;
                    _orgCosts.Add(_costsByOrgId[descr.SbmtOrgId]);
                }
                _costsByOrgId[descr.SbmtOrgId].Costs += descr.Scsts;

                if (_costsByDestination.ContainsKey(descr.Dests) == false)
                {
                    _costsByDestination.Add(descr.Dests, new ExpensesItem());
                    _costsByDestination[descr.Dests].Name = descr.Dests;
                    _destinationCosts.Add(_costsByDestination[descr.Dests]);
                }
                _costsByDestination[descr.Dests].Costs += descr.Scsts;

                foreach (string seniority in descr.Seniorities)
                {
                    if (_costsBySeniority.ContainsKey(seniority) == false)
                    {
                        _costsBySeniority.Add(seniority, new ExpensesItem());
                        _costsBySeniority[seniority].Name = seniority;
                        _seniorityCosts.Add(_costsBySeniority[seniority]);
                    }

                    _costsBySeniority[seniority].Costs += descr.Scsts;
                }
            }
        }

        Organization GetOrg(int id)
        {
            foreach (Organization organization in _organizations)
            {
                if(organization.Id == id)
                {
                    return organization;
                }
            }

            return null;
        }
    }

    public class ExpensesItem
    {
        private string name;
        private double costs = 0;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public double Costs
        {
            get { return costs; }
            set { costs = value; }
        }

        public string CostsText
        {
            get { return costs.ToString("n2"); }
        }
    }
}
